package event;

import javax.swing.*;
import java.io.File;

public class CoordinateProcessing {

    ImageProcessing imageProcessing = new ImageProcessing();


    public void getFiles(String pictureText, String outputText) {

        File f = new File(pictureText);
//        路径不存在
        if (!f.exists()) {
            return;
        }

        File[] result = f.listFiles();
        int MaxFile = 99;
        if (result != null && result.length > MaxFile) {
            MaxFile = result.length;
        }

//        图片名
        String[][] fileNames = new String[MaxFile][50];
//      文件夹名
        String[] files = new String[MaxFile];

        if (result != null) {
            int a = -1;
            int b = 0;
            String Name = "默认名";

            for (File value : result) {
                if (value.isFile()) {
                    String file = value.getName();
                    String fileName = file.substring(0, getCode(file));
                    if (!Name.equals(fileName)) {
                        Name = fileName;
                        a++;
                        b = 0;
                    }
                    if (b == 0) {
                        files[a] = Name;
                    }
                    fileNames[a][b] = file;
                    b++;
                }
            }
        }


//        迁移文件
        for (int i = 0; i < files.length; i++) {

            if (files[i] != null) {
                //            输出文件夹
                String outFolderName = outputText + "\\" + files[i];
                outFolderName = outFolderName.replace("\\", "/");
                for (int j = 0; j < 50; j++) {
                    if (fileNames[i][j] != null) {
                        //            图片地址
                        String pictureFolderName = pictureText + "\\" + fileNames[i][j];
                        pictureFolderName = pictureFolderName.replace("\\", "/");


                        System.out.println(pictureFolderName);

                        File afile = new File(pictureFolderName);
                        File outFolderDir = new File(outFolderName);
                        if (outFolderDir.mkdir()) {
                            System.out.println(outFolderName + "文件夹创建成功");
                        }
                        File outFolderFile = new File(outFolderDir + "\\" + afile.getName());

                        if (afile.renameTo(outFolderFile)) {
                            System.out.println(pictureFolderName + "移动成功");
                        } else {
                            System.out.println(pictureFolderName + "失败");
                        }
                    }

                }
            }


        }
        JOptionPane.showMessageDialog(null, "迁移完成");


    }

    /**
     * 截取村名索引
     *
     * @param fileName 字符串
     * @return 索引
     */
    public int getCode(String fileName) {
        //                遍历字符串
        for (int a = 0; a < fileName.length(); a++) {

            int byteAscii = imageProcessing.SumStrAscii(fileName.charAt(a) + "");
//            System.out.println(fileName.charAt(a) + "的byteAscii值" + byteAscii);
//                    读到空格就结束

            if (byteAscii == 32) {
                return a;
            }
            if (byteAscii == 46) {
                return a;
            }
            if (byteAscii == 45) {
                return a;
            }
        }
        return fileName.length();
    }
}
